package com.xquant.trade.utils;



import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class FileUtil {

    /**
     * 获取IP地址
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip != null && !"".equals(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (ip != null && !"".equals(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }

    public static long getLineNumber(File file) {
        if (file.exists()) {
            try {
                FileReader fileReader = new FileReader(file);
                LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
                lineNumberReader.skip(Long.MAX_VALUE);
                long lines = lineNumberReader.getLineNumber() + 1;
                fileReader.close();
                lineNumberReader.close();
                return lines;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 通过IP地址获取MAC地址
     *
     * @param ip String,127.0.0.1格式
     * @return mac String
     * @throws Exception
     */
    public static String getMACAddress(String ip) throws Exception {
        return "";
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


    public static String getMacAddress(String ip) {
        if ("127.0.0.1".equals(ip)) {

        }
        String macAddress = "";
        BufferedReader inputs = null;
        try {
            String scancmd = "nbtstat -A ";
            File file = new File("C:\\Windows\\SysWOW64");
            if (file.exists()) {
                scancmd = "C:\\Windows\\System32\\nbtstat.exe -A ";
            }
            String command = scancmd + ip;
            if ("127.0.0.1".equals(ip)) {
                command = "ipconfig -all ";
            }
            Process p = Runtime.getRuntime().exec(command);
            InputStreamReader ir = new InputStreamReader(
                    p.getInputStream(), "gbk");
            inputs = new BufferedReader(ir);
            String line;
            while ((line = inputs.readLine()) != null) {
                if (line.indexOf("MAC Address") > 0) {
                    macAddress = line.substring(
                            line.indexOf("MAC Address") + 14, line.length());
                }
                if (line.indexOf("MAC 地址") > 0) {
                    macAddress = line.substring(
                            line.indexOf("MAC Address") + 14, line.length());
                }
                if ("127.0.0.1".equals(ip) && line.indexOf("物理地址") > 0) {
                    macAddress = line.substring(
                            line.indexOf("物理地址") + 31, line.length());
                }
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return macAddress;
    }


    /**
     * 获取文件的创建时间：windows下可获取，linux下只能获取file.lastModified()
     *
     * @param filePath
     * @return
     */
    public static Long getFileCreateTime(String filePath) {
        File file = new File(filePath);
        try {
            Path path = Paths.get(filePath);
            BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
            BasicFileAttributes attr = basicview.readAttributes();
            return attr.creationTime().toMillis();
        } catch (Exception e) {
            e.printStackTrace();
            return file.lastModified();
        }
    }

    /*
     * 获取目录下所有的文件绝对路径
     *
     * @param homePath
     * @return
     */
    public static List<String> listFiles(String homePath) {
        List<String> savePathAndFile = new ArrayList<>();
        LinkedList<String> stack = new LinkedList<>();
        File homeDir = new File(homePath);
        if (homeDir.exists()) {
            stack.add(homePath);
        }
        while (!stack.isEmpty()) {
            String path = stack.pollLast();
            File file = new File(path);
            if (file.isDirectory()) {
                for (File f : file.listFiles()) {
                    stack.add(f.getAbsolutePath());
                }
            } else {
                savePathAndFile.add(file.getAbsolutePath());
            }
        }
        return savePathAndFile;
    }

    /**
     * 获取目录下所有的文件路径和对应文件的层数,控制查找的层数,非递归
     *
     * @param homePath
     * @return
     */
    public static List<String> listFiles(String homePath, int controlDeep) {
        List<String> savePathAndFile = new ArrayList<>();
        LinkedList<String> stack = new LinkedList<>();
        File homeDir = new File(homePath);
        if (homeDir.exists()) {
            stack.add(homePath);
        }
        int deep = 1;
        while (!stack.isEmpty() && deep <= controlDeep) {
            String path = stack.pollLast();
            File file = new File(path);
            if (file.isDirectory()) {
                for (File f : file.listFiles()) {
                    stack.add(f.getAbsolutePath());
                }
                ++deep;
            } else {
                savePathAndFile.add(file.getAbsolutePath());
            }
        }

        return savePathAndFile;
    }


    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    public static void main(String[] args) {
        List<String> fileEncode = listFiles("D:\\workxQant\\jtProject\\xIR_J2EE\\src\\main\\log");
        //String fileEncode2 = getFileEncode("D:\\GJ_CALC\\Common\\Log\\xetlLog.txt.8");
        //System.out.println("fileEncode2 = " + fileEncode2);
    }


}
